﻿#include "QQFriendIndexDelegate.h"

QQFriendIndexDelegate::QQFriendIndexDelegate(QObject* parent)
	: QStyledItemDelegate(parent)
{
}

QQFriendIndexDelegate::~QQFriendIndexDelegate()
{
}

void QQFriendIndexDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	QStyleOptionViewItem view_option(option);
	this->initStyleOption(&view_option, index);
	QRect item_rect = option.rect;

	painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);

	painter->save();
	item_rect.adjust(0, 2, 0, -2);
	QPainterPath path;
	path.addRoundedRect(item_rect, 4, 4);
	if (view_option.state & QStyle::State_Selected)
	{
		if (view_option.state & QStyle::State_MouseOver)
		{
			painter->fillPath(path, QColor("#0078D4"));
		}
		else
		{
			painter->fillPath(path, QColor("#0078D4"));
		}
	}
	else
	{
		if (view_option.state & QStyle::State_MouseOver)
		{
			painter->fillPath(path, ElaThemeColor(ElaThemeType::Light, BasicHoverAlpha));
		}
	}
	painter->restore();


	painter->save();
	QPixmap icon = index.data(Qt::UserRole + 1).value<QPixmap>();
	icon = icon.scaled(icon_width, icon_width, Qt::KeepAspectRatio, Qt::SmoothTransformation);
	QRect icon_rect = QRect(item_rect.x() + m_common_space / 2, item_rect.y() + item_rect.height() / 2 - icon_width / 2, icon_width, icon_width);
	painter->drawPixmap(icon_rect, icon);
	painter->restore();

	painter->save();
	if (view_option.state & QStyle::State_Selected)
	{
		painter->setPen(Qt::white);
	}
	else
	{
		painter->setPen(Qt::black);
	}
	QString name = index.data(Qt::UserRole + 2).toString();
	name = painter->fontMetrics().elidedText(name, Qt::ElideRight, name_size.width());
	QRect name_rect = QRect(item_rect.x() + icon_width + m_common_space, icon_rect.y(), name_size.width(), name_size.height());
	painter->drawText(name_rect, Qt::AlignLeft | Qt::TextWordWrap, name);
	painter->restore();
}